package com.example.myproject.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myproject.Sqlite.MySqliteOpenHelper;
import com.example.myproject.R;
import com.example.myproject.session.UserSession;

public class Login_activity extends AppCompatActivity implements View.OnClickListener {
    private Button login;
    private EditText username,password;
    private MySqliteOpenHelper mySqliteOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mySqliteOpenHelper = new MySqliteOpenHelper(this, "users", null, 1);
        find();
    }

    private void find() {
        login=findViewById(R.id.login);
        username=findViewById(R.id.login_username);
        password=findViewById(R.id.login_password);

        login.setOnClickListener(this);
    }



    @Override
    public void onClick(View view) {
        String username1 = username.getText().toString();
        String password1 = password.getText().toString();
        if(username1.isEmpty()||username1.length()<=0){
            Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
            return;
        }
        if(password1.isEmpty()||password1.length()<=0){
            Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
            return;
        }

        boolean login = mySqliteOpenHelper.login(username1, password1);
        if (login) {
            Toast.makeText(this, "登陆成功!", Toast.LENGTH_LONG).show();
            UserSession.setLoggedInUser(username1);
            Intent iLogin = new Intent(this, Home_activity.class);
            startActivity(iLogin);
        } else {
            Toast.makeText(this, "登陆失败!", Toast.LENGTH_SHORT).show();
        }
    }
}